package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.PertrolPriceEffectiveVO;
import com.zhiche.lisa.bms.service.driver.PertrolPriceEffectiveService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 加油站燃油价格生效表 前端控制器
 * </p>
 *
 * @author linbao
 * @since 2019-02-28
 */
@RestController
@Slf4j
@RequestMapping("/pertrolPrice")
@Api(description = "加油站燃油价格", tags = {"加油站燃油价格"})
public class PertrolPriceEffectiveController {

    @Autowired
    private PertrolPriceEffectiveService pertrolPriceEffectiveService;

    @PostMapping(value = "/queryPertrolPricePage")
    @ApiOperation(value = "分页获得加油站燃油价格", notes = "分页获得加油站燃油价格", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PertrolPriceEffectiveVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PertrolPriceEffectiveVO>> queryPertrolPricePage(
            @ApiParam(name = "FuelPriceEffectiveVO", value = "燃油价格VO", required = true)
            @RequestBody Page<PertrolPriceEffectiveVO> page) {
        log.info("PertrolPriceEffectiveController.queryPertrolPricePage params : {}", page);
        RestfulResponse<Page<PertrolPriceEffectiveVO>> result = getSuccess();
        try {
            result.setData(pertrolPriceEffectiveService.queryPertrolPricePage(page));
        } catch (BaseException e) {
            log.error("PertrolPriceEffectiveController.queryPertrolPricePage ERROR:{}", e);
            result = getError(e.getMessage());
        } catch (Exception e) {
            log.error("PertrolPriceEffectiveController.queryPertrolPricePage ERROR:{} ", e);
            result = getError(CommonEnum.ERROR.getText());
        }
        return result;
    }

    @PostMapping(value = "/addPertrolPrice")
    @ApiOperation(value = "添加加油站燃油价格", notes = "添加加油站燃油价格", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = String.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> addPertrolPrice(
            @ApiParam(name = "PertrolPriceEffectiveVO", value = "加油站燃油价格VO", required = true)
            @RequestBody @Valid PertrolPriceEffectiveVO pertrolPriceEffectiveVO, BindingResult br) {
        log.info("FuelPriceEffectiveController.add : {}", pertrolPriceEffectiveVO);
        RestfulResponse<String> result = getSuccess();
        try {
            pertrolPriceEffectiveService.addPertrolPrice(pertrolPriceEffectiveVO, br);
        } catch (BaseException e) {
            log.error("FuelPriceEffectiveController.addPertrolPrice ERROR:{}", e);
            result = getError(e.getMessage());
        } catch (Exception e) {
            log.error("FuelPriceEffectiveController.addPertrolPrice ERROR:{} ", e);
            result = getError(CommonEnum.ERROR.getText());
        }
        return result;
    }

    private RestfulResponse getSuccess() {
        return new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
    }

    private RestfulResponse getError(String errorMsg) {
        return new RestfulResponse<>(CommonEnum.ERROR.getCode(), errorMsg, null);
    }
}

